import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/Star.dart';

class TeacherInfo extends StatefulWidget {
  dynamic teacherData;

  TeacherInfo(this.teacherData);

  @override
  State<StatefulWidget> createState() {
    return _TeacherInfo();
  }
}

class _TeacherInfo extends State<TeacherInfo> {
  switchLike() {
    HttpGo(context).post("/Teacher/SetFavoriteTeacher", {
      "studentId": Global.studentId,
      "teacherId": widget.teacherData["teacherId"],
      "stateTag": widget.teacherData["isFavoriteTeacher"] ? 0 : 1,
    }, (data) {
      widget.teacherData["isFavoriteTeacher"] = !widget.teacherData["isFavoriteTeacher"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  playVoice() async {
    int result = await audioPlayer.play(widget.teacherData["voice"]);
    if (result == 1) {
      print('play success');
    } else {
      print('play failed');
      showToast('play failed');
    }
  }

  AudioPlayer audioPlayer = AudioPlayer();

  @override
  void deactivate() async {
    int result = await audioPlayer.release();
    if (result == 1) {
      print('release success');
    }
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    print(widget.teacherData);
    String canCourse = (widget.teacherData["teachingCourseList"] ?? []).join("、");
    String level = (widget.teacherData["teachingLevelList"] ?? []).join("、");
    String style = (widget.teacherData["teacherStyleList"] ?? []).join("、");
    return Container(
        decoration: Global.getWhiteBg(6),
        width: double.infinity,
        child: Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
          LocalImage("cross_gray.png", width: 20, padding: EdgeInsets.all(8), onTap: () {
            PageUtil().back(context);
          }),
          Expanded(
              child: SingleChildScrollView(
                  child: Container(
                      padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                      child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
                        Star(value: widget.teacherData["teacherGrade"] + 0.0,canChoose: false),
                        NetImage(widget.teacherData["photo"], width: 220, padding: EdgeInsets.fromLTRB(0, 8, 0, 16)),
                        Row(children: [
                          Text("${widget.teacherData["teacherName"]}",
                              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: MyColor.Orange)),
                          LocalImage("mic.png", width: 24, onTap: playVoice),
                          Expanded(child: Container()),
                          LocalImage(widget.teacherData["isFavoriteTeacher"] ? "like_on.png" : "like_off.png", width: 24, onTap: switchLike)
                        ]),
                        Container(height: 0.8, color: MyColor.Orange, margin: EdgeInsets.only(top: 16, bottom: 8)),
                        ItemInfo("教學年資：", "${widget.teacherData["expYear"]}"),
                        ItemInfo("可教授課程：", "$canCourse"),
                        ItemInfo("適合學員：", "$level"),
                        ItemInfo("教學風格：", "$style"),
                        ItemInfo("教學背景：", "${widget.teacherData["background"]}"),
                        ItemInfo("證照/證書：", "${widget.teacherData["certificate"]}"),
                        Container(
                            color: Color(0xfff1f1f1),
                            margin: EdgeInsets.fromLTRB(0, 8, 0, 16),
                            width: double.infinity,
                            padding: EdgeInsets.all(8),
                            child: Text("${widget.teacherData["selfIntro"]}",
                                style: TextStyle(height: 2, fontSize: 12, color: Color(0xff483115))))
                      ]))))
        ]));
  }
}

class ItemInfo extends StatelessWidget {
  String title;
  String content;

  ItemInfo(this.title, this.content);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
      child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Container(width: 100, child: Text("$title", style: TextStyle(fontSize: 12, color: Color(0xff483115), fontWeight: FontWeight.bold))),
        Expanded(child: Text("$content", style: TextStyle(fontSize: 12, color: Color(0xff483115))))
      ]),
    );
  }
}
